package session08.MyTest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PhotoFrame extends JFrame {

    private Container contentP;
    private JButton leftBtn;
    private JButton rightBtn;
    private Photo photo;

    public PhotoFrame() {
        this.setTitle("图片更换器");
        this.setSize(800, 500);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);//传另一个容器就相对于另一个容器，传null就相对于屏幕
        this.setResizable(false);//设置窗体大小不可变

        this.addContent();

        this.setVisible(true);
    }

    private void addContent() {
        this.contentP = this.getContentPane();//获取内容面板
        this.setLayout(new FlowLayout());

        this.photo = new Photo();

        this.leftBtn = new JButton("<<");
        this.leftBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout c = (CardLayout) PhotoFrame.this.photo.getLayout();
                c.previous(photo);
            }
        });
        this.rightBtn = new JButton(">>");
        this.rightBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout c = (CardLayout) PhotoFrame.this.photo.getLayout();
                c.next(photo);
            }
        });

        this.contentP.add(this.leftBtn);
        this.contentP.add(this.photo);
        this.contentP.add(this.rightBtn);
    }
}
